//
//  BEditShopDetaileViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/5/20.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BEditShopDetaileViewController.h"
#import "BshopMiaoshuViewController.h"
#import "CSnewCategoryViewController.h"
#import "PIc9CollectionViewCell.h"
#import "TZImagePickerController.h"
#import "EditExplainViewController.h"
#import "SYBigImage.h"

@interface BEditShopDetaileViewController ()<UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UITextViewDelegate,TZImagePickerControllerDelegate>
@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)UIButton *addpicbut;

@property(nonatomic,strong)UITextField *biaotiTF;
@property(nonatomic,strong)UITextField *priceTF;
@property(nonatomic,strong)UITextField *kucunTF;


@property(nonatomic,strong)UILabel *numberLB;
@property(nonatomic,strong)UILabel *fenleiLB;//产品分类

@property(nonatomic,strong)UIImage *seleImage;

@property(nonatomic,strong)NSString *fenleiId;

@property(nonatomic,strong)NSMutableArray *MiaoshuArr;
@property(nonatomic,strong)NSString *miaoshuStr;
@property(nonatomic,strong)NSString *delepicid;
@property(nonatomic,strong)UIView *bottBGview;

@property(nonatomic,strong)UIImage *myimage;


@property(nonatomic,strong)NSMutableArray *imageArr;
@property(nonatomic,strong)NSMutableArray *assets;
@property(nonatomic,strong)UICollectionView *mycollectView;

@property(nonatomic,strong)NSString *lbdelIds;

@property(nonatomic,strong)NSString *ididid;

@property(nonatomic,strong)NSString *isPingtai;


@property(nonatomic,strong)UIButton *ptzgBut;
@property(nonatomic,strong)UIButton *qtqdBut;

@property(nonatomic,strong)NSString *delebendi;


@property(nonatomic,strong)UIButton *ljfbBut;

@property(nonatomic,strong)UIView *seleBgview;//w九宫格没有图片时 z用这个盖着


@end

@implementation BEditShopDetaileViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
//    
//    [self preferredStatusBarStyle];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.ididid = _mymodel.Id;
    self.MiaoshuArr = [NSMutableArray arrayWithCapacity:1];
    
    self.imageArr = [NSMutableArray arrayWithCapacity:1];
    self.assets = [NSMutableArray arrayWithCapacity:1];
    [self showUi];
    self.isPingtai = @"2";
    _delepicid = @"";
    _lbdelIds = @"";
    
}
- (void)ExplinAction:(UIButton *)but
{
    [self.navigationController pushViewController:[EditExplainViewController new] animated:YES];
    
}
-(void)showUi{
    [self.view addSubview:self.scrollView];
    
    UIImageView *myheaderImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, kNaviHeight)];
    myheaderImage.image = imgname(@"shopHeadr");
    [self.scrollView addSubview:myheaderImage];
    
    UILabel *contitile = [[UILabel alloc]initWithFrame:CGRectMake(0, kNaviHeight-35, KWIDTH, 25)];
    contitile.font = FontSize(18);
    contitile.text = @"编辑产品";
    contitile.textColor = [UIColor whiteColor];
    contitile.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:contitile];
    
    UIButton *leftbut = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [leftbut setImage:imgname(@"whithReturnbut") forState:(UIControlStateNormal)];
    leftbut.frame = CGRectMake(5, kNaviHeight-40, 40, 40);
    [self.scrollView addSubview:leftbut];
    [leftbut addTarget:self action:@selector(fanhuiActin:) forControlEvents:(UIControlEventTouchUpInside)];
    UIButton *rightbut = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [rightbut setTitle:@"编辑说明" forState:(UIControlStateNormal)];
    rightbut.frame = CGRectMake(KWIDTH-62, kNaviHeight-40, 50, 40);
    rightbut.titleLabel.font = FontSize(12);
    [rightbut setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [self.scrollView addSubview:rightbut];
    [rightbut addTarget:self action:@selector(ExplinAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    UIView *bgpicView = [[UIView alloc]initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, 140)];
    bgpicView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    [self.scrollView addSubview:bgpicView];
    
//
//    UIButton *addpicbut = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    addpicbut.frame = CGRectMake(KWIDTH/2-30, kNaviHeight+18, 60, 60);
//    [addpicbut setImage:imgname(@"selePicbut") forState:(UIControlStateNormal)];
//    [addpicbut addTarget:self action:@selector(addpic:) forControlEvents:(UIControlEventTouchUpInside)];
//    self.addpicbut =addpicbut;
//    [self.scrollView addSubview:addpicbut];
//
//    UIButton *addpicbutlb = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    addpicbutlb.frame = CGRectMake(KWIDTH/2-50, kNaviHeight+18+66, 100, 20);
//    [addpicbutlb addTarget:self action:@selector(addpicbutlb:) forControlEvents:(UIControlEventTouchUpInside)];
//    [addpicbutlb setTitle:@"添加产品图片" forState:(UIControlStateNormal)];
//    [addpicbutlb setTitleColor:[UIColor colorWithHexString:@"#CECECE"] forState:(UIControlStateNormal)];
//    addpicbutlb.titleLabel.font = FontSize(14);
//    [self.scrollView addSubview:addpicbutlb];
//
    
    UIImageView *myimage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, (KWIDTH-6-32)/3, (KWIDTH-6-32)/3+12)];
    myimage.image = imgname(@"selebutnewblue");
   
    self.myimage = myimage.image;
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((KWIDTH-6-32)/3 , (KWIDTH-6-32)/3);
    //    _mycollectView.collectionViewLayout =layout;
    _mycollectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, 141) collectionViewLayout:layout];
    [self.scrollView addSubview:_mycollectView];
    //        layout.headerReferenceSize = CGSizeMake(KWIDTH, 204);
    layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 3);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    adjustInset(_mycollectView);
    _mycollectView.dataSource = self;
    _mycollectView.delegate = self;
    //    _mycollectView.backgroundColor =RGBA(237, 237, 237, 1);
    _mycollectView.backgroundColor = [UIColor whiteColor];
    [_mycollectView registerNib:[UINib nibWithNibName:@"PIc9CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PIc9CollectionViewCell"];
    
    if (_mymodel) {
        
    for (NSDictionary *mudic in _mymodel.lbImgs) {
        NSString *urlstr =[NSString stringWithFormat:@"%@",[mudic objectForKey:@"product_img_url"]];
        
        [_imageArr addObject:urlstr];
        
        
        
    }
      
        if (_imageArr.count<9) {
            [self.imageArr addObject: self.myimage];

        }
    }else{
        [self.imageArr addObject: self.myimage];

    }
        
    
    if (_imageArr.count<4) {
        _mycollectView.height= 141;
        
    }else if(_imageArr.count<7){
        _mycollectView.height = ((KWIDTH-6-32)/3+12)*2+16;
        
        
    }else{
        _mycollectView.height = ((KWIDTH-6-32)/3+12)*3+16;
        
    }
    
    

    
    [_mycollectView reloadData];
    
    
    
   _bottBGview = [[UIView alloc]initWithFrame:CGRectMake(0, bgpicView.bottom+16, KWIDTH, 485)];
    
    self.bottBGview.y =_mycollectView.bottom;

    [self.scrollView addSubview:_bottBGview];

    UILabel *biaotiLB = [[UILabel alloc]initWithFrame:CGRectMake(16, 0, 200, 20)];
    biaotiLB.font = FontSize(14);
    biaotiLB.textColor = [UIColor colorWithHexString:@"#CECECE"];
    biaotiLB.text = @"输入产品标题";
    [self.bottBGview addSubview:biaotiLB];

    UITextField *titlTf = [[UITextField alloc]initWithFrame:CGRectMake(16, biaotiLB.bottom+10, KWIDTH-10-55, 20)];
    self.biaotiTF = titlTf;
    self.biaotiTF.delegate =self;
//    titlTf.placeholder = @"请输入不大于30字的产品标题";

    titlTf.delegate = self;
    [self.bottBGview addSubview:titlTf];
    

    UILabel *numberl = [[UILabel alloc]initWithFrame:CGRectMake(titlTf.right, biaotiLB.bottom+10, 40, 20)];
    numberl.font = FontSize(14);
    numberl.textColor = [UIColor colorWithHexString:@"#CECECE"];
    numberl.text = @"0/30";
    if (_mymodel) {
        NSInteger ind=  [_mymodel.product_name length];
        numberl.text = [NSString stringWithFormat:@"%ld/%ld",ind,30-ind];
    }
    self.numberLB = numberl;
    [self.bottBGview addSubview:numberl];
    
    UILabel *lon0 = [[UILabel alloc]initWithFrame:CGRectMake(16, titlTf.bottom+10, KWIDTH-16, 1)];
    lon0.backgroundColor = [UIColor colorWithHexString:@"#EDEDED"];
    [self.bottBGview addSubview:lon0];
    
    UILabel *fenlei = [[UILabel alloc]initWithFrame:CGRectMake(16, lon0.bottom+16, 70, 22)];
    fenlei.font = FontSize(16);
    fenlei.textColor = [UIColor colorWithHexString:@"#666666"];
    fenlei.text = @"产品分类";
    [self.bottBGview addSubview:fenlei];
    
    
    UIButton *selefenlai = [UIButton buttonWithType:(UIButtonTypeCustom)];
    selefenlai.frame = CGRectMake(KWIDTH-50, lon0.bottom+16, 40, 20);
    [selefenlai setImage:imgname(@"arrow_right_icon") forState:(UIControlStateNormal)];
    [selefenlai addTarget:self action:@selector(selefenlai:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.bottBGview addSubview:selefenlai];
    
    UILabel *fenleilb = [[UILabel alloc]initWithFrame:CGRectMake(fenlei.right, lon0.bottom+16, KWIDTH-56-fenlei.right, 20)];
    fenleilb.font = FontSize(16);
    fenleilb.textColor = [UIColor colorWithHexString:@"#666666"];
    self.fenleiLB = fenleilb;
    fenleilb.textAlignment = NSTextAlignmentRight;
    [self.bottBGview addSubview:fenleilb];
    
    UILabel *lon001 = [[UILabel alloc]initWithFrame:CGRectMake(16, fenlei.bottom+10, KWIDTH-16, 1)];
    lon001.backgroundColor = [UIColor colorWithHexString:@"#EDEDED"];
    [self.bottBGview addSubview:lon001];
    
    UILabel *goodsqd = [[UILabel alloc]initWithFrame:CGRectMake(16, lon001.bottom+16, 70, 22)];
    goodsqd.font = FontSize(16);
    goodsqd.textColor = [UIColor colorWithHexString:@"#666666"];
    goodsqd.text = @"产品渠道";
    [self.bottBGview addSubview:goodsqd];
    
    UIButton *pingtaizhigong = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.bottBGview addSubview:pingtaizhigong];
    [pingtaizhigong setImage:imgname(@"unselebutim") forState:(UIControlStateNormal)];
    [pingtaizhigong setTitle:@"侯保产品" forState:(UIControlStateNormal)];
    [pingtaizhigong setTitleColor:K666666 forState:(UIControlStateNormal)];
    [pingtaizhigong addTarget:self action:@selector(ptzgAction:) forControlEvents:(UIControlEventTouchUpInside)];
    pingtaizhigong.titleLabel.font = FontSize(14);

    UIButton *qitaqudao = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.bottBGview addSubview:qitaqudao];
    [qitaqudao setImage:imgname(@"slebutim") forState:(UIControlStateNormal)];
    [qitaqudao setTitle:@"其它渠道" forState:(UIControlStateNormal)];
    [qitaqudao addTarget:self action:@selector(qitaqudaoaction:) forControlEvents:(UIControlEventTouchUpInside)];
    [qitaqudao setTitleColor:K666666 forState:(UIControlStateNormal)];
    self.ptzgBut =pingtaizhigong;
    self.qtqdBut =qitaqudao;
    qitaqudao.titleLabel.font = FontSize(14);
    qitaqudao.frame = CGRectMake(KWIDTH-16-95, goodsqd.frame.origin.y, 95, 30);
    pingtaizhigong.frame = CGRectMake(KWIDTH-16-95-95-29, goodsqd.frame.origin.y, 95, 30);

//    [qitaqudao mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.offset(-16);
//        make.height.offset(30);
//        make.width.offset(95);
//        make.centerY.mas_equalTo(goodsqd.mas_centerY).offset(0);
//    }];
    
//    [pingtaizhigong mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(qitaqudao.mas_left).offset(-29);
//        make.height.offset(30);
//        make.width.offset(95);
//        make.centerY.mas_equalTo(goodsqd.mas_centerY).offset(0);
//    }];
    
    
    
    
    
    
    
    UIView *bgpicView1 = [[UIView alloc]initWithFrame:CGRectMake(0, goodsqd.bottom+12, KWIDTH, 14)];
    bgpicView1.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    [self.bottBGview addSubview:bgpicView1];
    
    UILabel *price = [[UILabel alloc]initWithFrame:CGRectMake(16, bgpicView1.bottom+15, 70, 22)];
    price.font = FontSize(16);
    price.textColor = [UIColor colorWithHexString:@"#666666"];
    price.text = @"价格";
    [self.bottBGview addSubview:price];
    
    
    UITextField *pricet = [[UITextField alloc]initWithFrame:CGRectMake(price.right, bgpicView1.bottom+13, KWIDTH-20-price.right, 22)];
    self.priceTF = pricet;
    pricet.delegate = self;
    pricet.keyboardType = UIKeyboardTypeNumberPad;
    pricet.textAlignment = NSTextAlignmentRight;
//    pricet.placeholder = @"请输入产品价格";
    pricet.text = @"¥";
    [pricet addRules];
    [self.bottBGview addSubview:pricet];
    
    UILabel *renminbi = [[UILabel alloc]initWithFrame:CGRectMake(KWIDTH-31, bgpicView1.bottom+15, 15, 22)];
    renminbi.font = FontSize(14);
    renminbi.textColor = [UIColor colorWithHexString:@"#0D0E10"];
    renminbi.text = @"¥";
//    [self.scrollView addSubview:renminbi];

    UILabel *lon1 = [[UILabel alloc]initWithFrame:CGRectMake(16, pricet.bottom+10, KWIDTH-16, 1)];
    lon1.backgroundColor = [UIColor colorWithHexString:@"#EDEDED"];
    [self.bottBGview addSubview:lon1];
    UILabel *kecunlb = [[UILabel alloc]initWithFrame:CGRectMake(16, lon1.bottom+16, 70, 22)];
    kecunlb.font = FontSize(16);
    kecunlb.textColor = [UIColor colorWithHexString:@"#666666"];
    kecunlb.text = @"库存";

    [self.bottBGview addSubview:kecunlb];
    
    UITextField *kucun = [[UITextField alloc]initWithFrame:CGRectMake(price.right, lon1.bottom+16, KWIDTH-20-price.right, 22)];
    self.kucunTF = kucun;
    kucun.delegate = self;
    kucun.keyboardType = UIKeyboardTypeNumberPad;
//    kucun.placeholder = @"请输入库存";
    [self.bottBGview addSubview:kucun];
    kucun.textAlignment = NSTextAlignmentRight;

    UIView *bgpicView2 = [[UIView alloc]initWithFrame:CGRectMake(0, kecunlb.bottom+10, KWIDTH, 14)];
    bgpicView2.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    [self.bottBGview addSubview:bgpicView2];
    
    
    
    UILabel *chanpinmiaoshu = [[UILabel alloc]initWithFrame:CGRectMake(16, bgpicView2.bottom+16, 70, 20)];
    chanpinmiaoshu.font = FontSize(16);
    chanpinmiaoshu.textColor = [UIColor colorWithHexString:@"#666666"];
    chanpinmiaoshu.text = @"产品描述";
    [self.bottBGview addSubview:chanpinmiaoshu];
    
    
    UIButton *chanpinmiaoshubut = [UIButton buttonWithType:(UIButtonTypeCustom)];
    chanpinmiaoshubut.frame = CGRectMake(KWIDTH-50, bgpicView2.bottom+16, 40, 20);
    [chanpinmiaoshubut setImage:imgname(@"arrow_right_icon") forState:(UIControlStateNormal)];
    [chanpinmiaoshubut addTarget:self action:@selector(chactanpinmiaoshubut:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.bottBGview addSubview:chanpinmiaoshubut];
    
    CGFloat ffdfs = KHEIGHT - chanpinmiaoshu.bottom - 16-kNaviHeight;
    if (ffdfs <153) {
        ffdfs = 153;
    }

    UIView *bgpicView3 = [[UIView alloc]initWithFrame:CGRectMake(0, chanpinmiaoshu.bottom+16, KWIDTH, ffdfs-43)];
    bgpicView3.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    [self.bottBGview addSubview:bgpicView3];
    
    UIButton *lijifabu = [UIButton buttonWithType:(UIButtonTypeCustom)];
    lijifabu.frame = CGRectMake(0, chanpinmiaoshu.bottom+64, KWIDTH, 43);
    [lijifabu setTitle:@"立即发布" forState:(UIControlStateNormal)];
    [lijifabu setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [lijifabu setBackgroundColor:[UIColor colorWithHexString:@"#70BE68"]];
    [lijifabu addTarget:self action:@selector(lijifabuact:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.bottBGview addSubview:lijifabu];
    
    
    
    
    
//    self.scrollView.contentSize = CGSizeMake(KWIDTH, lijifabu.bottom);
    self.scrollView.contentSize = CGSizeMake(KWIDTH, self.bottBGview.bottom+kTabbarHeight);
    
    if (_mymodel) {
        UIImageView *pic = [[UIImageView alloc]init];
        [pic sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_mymodel.product_pic]]];
        self.seleImage =pic.image;
        [self.addpicbut setImage:self.seleImage forState:(UIControlStateNormal)];
        self.biaotiTF.text = _mymodel.product_name;
        self.priceTF.text = [NSString stringWithFormat:@"¥%@",_mymodel.product_price];
        self.kucunTF.text = _mymodel.Stock;

        self.fenleiLB.text = _mymodel.type_name;
     _fenleiId = _mymodel.product_type_id;
        _miaoshuStr =_mymodel.product_info;
        self.fenleiLB.textColor = K333333;

        for (NSDictionary *mudic in _mymodel.imgs) {
            NSString *urlstr =[NSString stringWithFormat:@"%@",[mudic objectForKey:@"product_img_url"]];
            
            [_MiaoshuArr addObject:urlstr];
            
            
            
        }
        
    }
}
-(void)ptzgAction:(UIButton *)but{
    self.isPingtai = @"1";
    [self.ptzgBut setImage:imgname(@"slebutim") forState:(UIControlStateNormal)];
    [self.qtqdBut setImage:imgname(@"unselebutim") forState:(UIControlStateNormal)];

}

-(void)qitaqudaoaction:(UIButton *)but{
    self.isPingtai = @"2";
    [self.qtqdBut setImage:imgname(@"slebutim") forState:(UIControlStateNormal)];
    [self.ptzgBut setImage:imgname(@"unselebutim") forState:(UIControlStateNormal)];

}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField == self.priceTF) {
        NSString  *str  =self.priceTF.text;
        
        if (![str hasPrefix:@"¥"]) {
            str = [NSString stringWithFormat:@"¥%@",str];
            self.priceTF.text =str;
        }
        
        
        
    }
    
    return YES;
}

-(void)xiugai{
    if (_MiaoshuArr.count == 0) {
        ShowToastWithText(@"请至少选择一种产品描述图片");
        return;
    }
    kWeakSelf;
    if (_imageArr.count == 1) {
        ShowToastWithText(@"请至少选择一种产品图片");
        return;
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
    dic[@"product_name"] =_biaotiTF.text;
    dic[@"product_type_id"] =_fenleiId;
    NSString *price = _priceTF.text;
    if ([price hasPrefix:@"¥"]) {
        
    }
    while ([price hasPrefix:@"¥"]) {
        price = [price substringFromIndex:1];
    }
    dic[@"product_price"] =price;
    dic[@"product_info"] =_miaoshuStr;
    dic[@"Stock"] =_kucunTF.text;
    if (!strIsEmpty(_delepicid)) {
        dic[@"delIds"] =[_delepicid substringFromIndex:1];
      
    }
    if (!strIsEmpty(_lbdelIds)) {
        dic[@"delLbIds"] =[_lbdelIds substringFromIndex:1];
        
    }
    dic[@"id"] =_mymodel.Id;
    NSString *deleStr = @"";
    for (int i = 0; i <_imageArr.count; i++) {
        
        if ([_imageArr[i] isKindOfClass:[NSString class]]) {
            [_imageArr removeObjectAtIndex:i];
        }else{
            
            deleStr = [NSString stringWithFormat:@"%@,%@",deleStr,_imageArr[i]];
            
        }
        
    }
//    if (deleStr.length>3) {
//        dic[@"delIds"] =deleStr;
//
//    }

    NSString *dadsf = [HFTools toJSONString:dic];
    NSMutableDictionary *apram = [NSMutableDictionary dictionaryWithCapacity:1];
    apram[@"data"] =dadsf;
    
   
    NSMutableArray *miaoshuArr = [NSMutableArray arrayWithCapacity:1];
    
    if (_MiaoshuArr.count>0) {
        
        if ([_MiaoshuArr containsObject:_seleImage]) {
            [_MiaoshuArr removeObject:_seleImage];
        }
        
    for (int i = 0; i <_MiaoshuArr.count ; i++) {
        
        if ([_MiaoshuArr[i] isKindOfClass:[NSString class]]) {
            [_MiaoshuArr removeObjectAtIndex:i];
            
        }else if( _MiaoshuArr[i] == _seleImage){
            [_MiaoshuArr removeObjectAtIndex:i];

        }else{
            
            [miaoshuArr addObject:_MiaoshuArr[i]];
        }
    }
        
    }
 
    
  
    if (miaoshuArr.count >0) {
        
        
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool apppicUploadPicWithUrl:BupdateCellProdct param:apram key:@"date" image:miaoshuArr withSuccess:^(id dic) {
        [weakSelf uoimagewit];

//        ShowToastWithText(@"修改成功");
   [[NSNotificationCenter defaultCenter] postNotificationName:@"refashA" object:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refashB" object:nil];

//        [weakSelf.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        
    }];
    }else{
        [NetWorkTool POST:BupdateCellProdct param:apram success:^(id dic) {
            [weakSelf uoimagewit];
                    ShowToastWithText(@"修改成功");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refashA" object:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refashB" object:nil];

        } other:^(id dic) {
            ShowToastWithText(dic[@"msg"]);
        } fail:^(NSError *error) {
            
        } needUser:YES];
        
    }
}

-(void)lijifabuact:(UIButton *)liji{
    
  
    //立即发布
    if (strIsEmpty(_biaotiTF.text)) {
        ShowToastWithText(@"请输入产品标题");
        return;
    }
    if (strIsEmpty(_fenleiId)) {
        ShowToastWithText(@"请选择产品分类");
        return;
    }
    if (strIsEmpty(self.isPingtai)) {
        ShowToastWithText(@"请选择产品渠道");
        return;
    }
    if (strIsEmpty(_priceTF.text)) {
        ShowToastWithText(@"请输入产品价格");
        return;
    }
    if (strIsEmpty(_miaoshuStr)) {
        ShowToastWithText(@"请输入文字介绍");
        return;
    }
    if (strIsEmpty(_kucunTF.text)) {
        ShowToastWithText(@"请输入产品库存");
        return;
    }
    if (self.mymodel) {
        [self xiugai];
        return;
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
    dic[@"product_name"] =_biaotiTF.text;
    dic[@"product_type_id"] =_fenleiId;
    NSString *price =[_priceTF.text substringFromIndex:1];
    while ([price hasPrefix:@"¥"]) {
        price = [price substringFromIndex:1];
    }
    dic[@"product_price"] =price;
    dic[@"product_info"] =_miaoshuStr;
    dic[@"Stock"] =_kucunTF.text;
    dic[@"channel"] =self.isPingtai;

    NSString *dadsf = [HFTools toJSONString:dic];
    NSMutableDictionary *apram = [NSMutableDictionary dictionaryWithCapacity:1];
    apram[@"data"] =dadsf;
    
   
//    if (_seleImage) {
//        [_MiaoshuArr insertObject:_seleImage atIndex:0];
//
//    }
    kWeakSelf;
    if (_MiaoshuArr.count == 0) {
        ShowToastWithText(@"请至少选择一种产品描述图片");
        return;
    }
    
    
    if (_imageArr.count == 1) {
        ShowToastWithText(@"请至少选择一种产品图片");
        return;
    }else{
        [_imageArr removeLastObject];
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

   [NetWorkTool apppicUploadPicWithUrl:BshopaddCellProdct param:apram key:@"date" image:_imageArr withSuccess:^(id dic) {
//       ShowToastWithText(@"发布成功");
       weakSelf.ididid = [NSString stringWithFormat:@"%@",[dic objectForKey:@"data"]];
       [[NSNotificationCenter defaultCenter] postNotificationName:@"refashA" object:nil];
       [[NSNotificationCenter defaultCenter] postNotificationName:@"refashB" object:nil];

       [weakSelf uoimagewit];

//       [weakSelf.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        
        
    }];
    
    
    
}
-(void)uoimagewit{
    NSMutableArray *immuarr = [NSMutableArray arrayWithCapacity:1];
    
    for (int i = 0; i <_MiaoshuArr.count; i++) {
        
        if ([_MiaoshuArr[i] isKindOfClass:[NSString class]]) {
            [_MiaoshuArr removeObjectAtIndex:i];
            
        }else{
         
            UIImageView *myimage = [[UIImageView alloc]initWithImage:_MiaoshuArr[i]];
            if (myimage.image != _seleImage) {
            [immuarr addObject:myimage];
            }
        }
        
        
        
        
    }
  
    kWeakSelf;
    if (immuarr.count > 0) {
   
        NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
        param[@"id"] = NOTNIL(_ididid);
    
    
    [NetWorkTool UploadPicWithUrl:BeMyShopaddProductLbImg param:param key:@"ima" image:immuarr withSuccess:^(id dic) {
        ShowToastWithText(@"发布成功");
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {

        [MBProgressHUD hideHUDForView:self.view animated:YES];

    }];
    }else{
        
        ShowToastWithText(@"发布成功");
        [MBProgressHUD hideHUDForView:self.view animated:YES];

        [weakSelf.navigationController popViewControllerAnimated:YES];
        return;
    
    }
    
    
}

-(void)chactanpinmiaoshubut:(UIButton *)but{
    //产品描述
    
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    BshopMiaoshuViewController *miaoshu = [[BshopMiaoshuViewController alloc]init];
    if (_mymodel) {
        miaoshu.model = _mymodel;
    }
    
    kWeakSelf;
    miaoshu.myblock = ^(NSArray * _Nonnull imagearr, NSString * _Nonnull miaoshu, NSString * _Nonnull deledid) {
        [self.MiaoshuArr addObjectsFromArray:imagearr];
        self.miaoshuStr =miaoshu;
        self.delepicid =deledid;
    };
    [self.navigationController pushViewController:miaoshu animated:YES];
}
-(void)selefenlai:(UIButton *)fenlei{
    //选择分类
    CSnewCategoryViewController *fenleivc = [[CSnewCategoryViewController alloc]init];
    kWeakSelf;
    
    fenleivc.myblock = ^(NSString * _Nonnull str, NSString * _Nonnull idstr) {
        weakSelf.fenleiLB.text = str;
        weakSelf.fenleiLB.textColor = K333333;
        weakSelf.fenleiId = idstr;
    };
    
[self.navigationController pushViewController:fenleivc animated:YES];
    
}
-(void)addpicbutlb:(UIButton *)but{
    //添加图片
    [self addpic];
}
-(void)addpic:(UIButton *)but{
    //添加图片
    UIImagePickerController *pick = [[UIImagePickerController alloc]init];
    pick.allowsEditing = YES;
    pick.delegate = self;
    [UIAlertController showPhotoAlerControllerPicker:pick controller:self sourceView:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *image = info[UIImagePickerControllerEditedImage];
    [self.addpicbut setImage:image forState:(UIControlStateNormal)];
    self.seleImage = image;
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == self.biaotiTF) {
//        NSString *sdfsfsdf = [NSString stringWithFormat:@"%@%@",textField.text,string];
        
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        


        NSInteger ind =newString.length;
            ind = 30-ind;
        if (ind < 0) {
            ind = 0;
        }
            self.numberLB.text = [NSString stringWithFormat:@"%ld/30",ind];
        
        
        if (newString.length <31) {
            return YES;
        }else{
            return NO;
        }
        
    }else{
        NSArray *attt = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"."];
        if (![attt containsObject:string]  && ![string  isEqualToString:@""]) {
            ShowToastWithText(@"只能输入数字");
            return NO;

        }else{
            return YES;

        }
    }
}
-(void)addpic{
    [self addpic];
    
}
-(void)fanhuiActin:(UIButton *)but{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (UIScrollView *)scrollView {
if (!_scrollView) {
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
    _scrollView.backgroundColor = [UIColor whiteColor];
    _scrollView.delegate = self;
    adjustInset(_scrollView);
}
return _scrollView;
    
}













- ( CGSize )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath {
    return CGSizeMake((KWIDTH-6-32)/3 , (KWIDTH-6-32)/3);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (_imageArr.count > 9) {
        return 9;
    }else{
        
        return _imageArr.count;
    }
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_imageArr.count == 10) {
        ShowToastWithText(@"最多选择9张");
        return;
    }
    
    if(indexPath.row == (_imageArr.count-1)){
        NSInteger inddd = 9 ;
        if (_mymodel) {
            inddd = 9 - _mymodel.lbImgs.count;
        }
        
        
        TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:inddd columnNumber:4 delegate:self pushPhotoPickerVc:YES];
        imagePickerVc.sortAscendingByModificationDate = NO;
        imagePickerVc.selectedAssets = _assets;
        [self presentViewController:imagePickerVc animated:YES completion:nil];
    }else{
        //查看大图
//        SYBigImage * bigI = [[SYBigImage alloc]init];
//        [_myconImage addGestureRecognizer:bigI];
//        
    }
    
}
#pragma mark - TZImagePickerControllerDelegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    if ([picker isKindOfClass:[UIImagePickerController class]]) {
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)tz_imagePickerControllerDidCancel:(TZImagePickerController *)picker {
}

// 当选择器自己dismiss的时候，会执行下面的代理方法
// isSelectOriginalPhoto 选择了原图
// asset通过：[[TZImageManager manager] getOriginalPhotoWithAsset:completion:]获得原图
// photos数组里的UIImage对象，默认是828像素宽，你可以通过设置photoWidth属性的值来改变它
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    if (photos.count == 0) {
        return;
    }

    [_imageArr removeAllObjects];
    [_assets removeAllObjects];
    

    if (_mymodel) {
        [_imageArr removeAllObjects];

        for (NSDictionary *mudic in _mymodel.lbImgs) {
            NSString *urlstr =[NSString stringWithFormat:@"%@",[mudic objectForKey:@"product_img_url"]];

            [_imageArr addObject:urlstr];



        }
    }


    
    
    
    
    
    [_imageArr addObjectsFromArray:photos];
    
    
    
    
    
    [_imageArr addObject:_myimage];
    
    [_assets addObjectsFromArray:assets];
    [self.mycollectView reloadData];
    
    if (_imageArr.count<4) {
        _mycollectView.height= 141;
        
    }else if(_imageArr.count<7){
        _mycollectView.height = ((KWIDTH-6-32)/3+12)*2+16;
        
        
    }else{
        _mycollectView.height = ((KWIDTH-6-32)/3+12)*3+16;
        
    }
    
    CGFloat yyyy = kNaviHeight+  _mycollectView.height +16;

    self.bottBGview.y =_mycollectView.bottom;
    self.scrollView.contentSize = CGSizeMake(KWIDTH, self.bottBGview.bottom+kTabbarHeight);

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_imageArr.count == 1) {
        [self.scrollView addSubview:self.seleBgview];
    }else{
        [self.seleBgview removeFromSuperview];
    }
    
    PIc9CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PIc9CollectionViewCell" forIndexPath:indexPath];
    cell.deleAction.hidden = NO;
    cell.myimageLable.hidden = YES;
    if ([[_imageArr safeObjectAtIndex:indexPath.row] isKindOfClass:[UIImage class]]) {
        cell.myconImage.image = [_imageArr safeObjectAtIndex:indexPath.row];
        
    }else{
        
        NSString *strdf =[_imageArr safeObjectAtIndex:indexPath.row];
        [cell.myconImage sd_setImageWithURL:[NSURL URLWithString:strdf]];
    }
    if (_imageArr.count != 10) {
        if(indexPath.row == (_imageArr.count-1)){
            cell.deleAction.hidden = YES;
            cell.myimageLable.hidden = NO;
            NSInteger inddd = _imageArr.count-1;
            cell.myimageLable.text = [NSString stringWithFormat:@"%ld/9",inddd];
        }
    }

    if(indexPath.row == (_imageArr.count-1)){
       
    }else{
        //查看大图
                SYBigImage * bigI = [[SYBigImage alloc]init];
                [cell.myconImage addGestureRecognizer:bigI];
        
    }
    
    
    cell.myblock = ^(NSString * _Nonnull str) {
        NSString * sdfsd = [NSString stringWithFormat:@"%ld",indexPath.row];
        [self delete:sdfsd];
    };
    return cell;
}

-(void)delete:(NSString *)ind{
    
    NSInteger indd = [ind integerValue];
    if (self.mymodel) {
        NSMutableArray *arr =[self.mymodel.lbImgs mutableCopy];
        if (arr.count > indd) {
            NSDictionary *dafa = _mymodel.lbImgs[indd];
    _lbdelIds = [NSString stringWithFormat:@"%@,%@",_lbdelIds,[dafa objectForKey:@"id"]];
            [_imageArr removeObjectAtIndex:indd];
            [arr removeObjectAtIndex:indd];
            self.mymodel.lbImgs = arr;
        }else{
            //删除本地
            [_imageArr removeObjectAtIndex:indd];
            [_assets removeObjectAtIndex:indd-self.mymodel.lbImgs.count];
            
        }
    }
    else{
        //删除本地
        [_imageArr removeObjectAtIndex:indd];
        [_assets removeObjectAtIndex:indd];


    }
    
    
    [_mycollectView reloadData];
    
    if (_imageArr.count<4) {
        _mycollectView.height= 141;
        
    }else if(_imageArr.count<7){
        _mycollectView.height = ((KWIDTH-6-32)/3+12)*2+16;
        
        
    }else{
        _mycollectView.height = ((KWIDTH-6-32)/3+12)*3+16;
        
    }
    
//    CGFloat yyyy = kNaviHeight+  _mycollectView.height +16;

    self.bottBGview.y =_mycollectView.bottom;
    
//    self.bottomView.height =yyyy;
    self.scrollView.contentSize = CGSizeMake(KWIDTH, self.bottBGview.bottom+kTabbarHeight);
    
    
    
    
}
-(UIView *)seleBgview{
    if (!_seleBgview) {
        _seleBgview = [[UIView alloc]initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, 140)];
        _seleBgview.backgroundColor = [UIColor colorWithHexString:@"#FFF2F2F2"];
        _seleBgview.userInteractionEnabled = YES;
        UIButton *addpicbut = [UIButton buttonWithType:(UIButtonTypeCustom)];
            addpicbut.frame = CGRectMake(KWIDTH/2-30, 18, 60, 60);
            [addpicbut setImage:imgname(@"selePicbut") forState:(UIControlStateNormal)];
            [addpicbut addTarget:self action:@selector(addpicaction:) forControlEvents:(UIControlEventTouchUpInside)];
            self.addpicbut =addpicbut;
            [_seleBgview addSubview:addpicbut];
        
            UIButton *addpicbutlb = [UIButton buttonWithType:(UIButtonTypeCustom)];
            addpicbutlb.frame = CGRectMake(KWIDTH/2-50, 18+66, 100, 20);
            [addpicbutlb addTarget:self action:@selector(addpicbutlbaction:) forControlEvents:(UIControlEventTouchUpInside)];
            [addpicbutlb setTitle:@"添加产品图片" forState:(UIControlStateNormal)];
            [addpicbutlb setTitleColor:[UIColor colorWithHexString:@"#CECECE"] forState:(UIControlStateNormal)];
            addpicbutlb.titleLabel.font = FontSize(14);
        [_seleBgview addSubview:addpicbutlb];
        
    }
    return _seleBgview;
}
-(void)addpicaction:(UIButton *)but{
    
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount: 9 columnNumber:4 delegate:self pushPhotoPickerVc:YES];
    imagePickerVc.sortAscendingByModificationDate = NO;
    imagePickerVc.selectedAssets = _assets;
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}
-(void)addpicbutlbaction:(UIButton *)but{
    
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount: 9 columnNumber:4 delegate:self pushPhotoPickerVc:YES];
    imagePickerVc.sortAscendingByModificationDate = NO;
    imagePickerVc.selectedAssets = _assets;
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}
@end
